package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.LbsTerminalStateRpcService;
import com.hunttown.mes.rpc.domain.LbsTerminalStateDTO;
import com.hunttown.mes.rpc.domain.query.LbsTerminalStateDTOQuery;
import com.hunttown.mes.service.LbsTerminalStateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.LbsTerminalStateConverts.convert;

/**
 * created by wangjunfu on 2022-08-08 09:47:29
 */
@Service
public class LbsTerminalStateRpcServiceImpl implements LbsTerminalStateRpcService {

    private final Logger logger = LoggerFactory.getLogger(LbsTerminalStateRpcServiceImpl.class);

    private final LbsTerminalStateService lbsTerminalStateService;

    @Autowired
    public LbsTerminalStateRpcServiceImpl(LbsTerminalStateService lbsTerminalStateService) {
        this.lbsTerminalStateService = lbsTerminalStateService;
    }

    @Override
    public LbsTerminalStateDTO insert(LbsTerminalStateDTO lbsTerminalStateDTO) {
        try {
            return convert(lbsTerminalStateService.insert(convert(lbsTerminalStateDTO)));
        } catch (Exception e) {
            logger.error("LbsTerminalState create failed.", e);
            throw new RuntimeException("LbsTerminalState create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(LbsTerminalStateDTO lbsTerminalStateDTO) {
        try {
            return lbsTerminalStateService.updateInfoById(convert(lbsTerminalStateDTO));
        } catch (Exception e) {
            logger.error("LbsTerminalState update by id failed.", e);
            throw new RuntimeException("LbsTerminalState update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(LbsTerminalStateDTOQuery lbsTerminalStateDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalStateDTOQuery.getW_id() != null && lbsTerminalStateDTOQuery.getW_id() > 0) {
                query.put("w_id", lbsTerminalStateDTOQuery.getW_id());
            }

            if (lbsTerminalStateDTOQuery.getEntId() != null && lbsTerminalStateDTOQuery.getEntId() > 0) {
                query.put("w_entId", lbsTerminalStateDTOQuery.getEntId());
            }

            if (lbsTerminalStateDTOQuery.getW_terminalId() != null) {
                query.put("w_terminalId", lbsTerminalStateDTOQuery.getW_terminalId());
            }

            if (lbsTerminalStateDTOQuery.getIds() != null && !lbsTerminalStateDTOQuery.getIds().equals("")) {
                query.put("ids", lbsTerminalStateDTOQuery.getIds());
            }

            if (lbsTerminalStateDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalStateDTOQuery.getDeleteFlag());
            }

            return lbsTerminalStateService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("LbsTerminalState update by query failed.", e);
            throw new RuntimeException("LbsTerminalState update by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalStateDTO selectById(Integer id) {
        try {
            return convert(lbsTerminalStateService.selectById(id));
        } catch (Exception e) {
            logger.error("LbsTerminalState select by id failed.", e);
            throw new RuntimeException("LbsTerminalState select by id failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalStateDTO selectObjByQuery(LbsTerminalStateDTOQuery lbsTerminalStateDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalStateDTOQuery.getEntId() != null) {
                query.put("entId", lbsTerminalStateDTOQuery.getEntId());
            }

            if (lbsTerminalStateDTOQuery.getTerminalId() != null) {
                query.put("terminalId", lbsTerminalStateDTOQuery.getTerminalId());
            }

            if (lbsTerminalStateDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalStateDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsTerminalStateDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsTerminalStateDTOQuery.getOrderBy());
            }

            return convert(lbsTerminalStateService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("LbsTerminalState select obj by query failed.", e);
            throw new RuntimeException("LbsTerminalState select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<LbsTerminalStateDTO> getListForPage(LbsTerminalStateDTOQuery lbsTerminalStateDTOQuery) {
        Query query = Query.page(lbsTerminalStateDTOQuery.getPn(), lbsTerminalStateDTOQuery.getPageSize());

        if (lbsTerminalStateDTOQuery.getId() != null && lbsTerminalStateDTOQuery.getId() > 0) {
            query.put("id", lbsTerminalStateDTOQuery.getId());
        }

        if (lbsTerminalStateDTOQuery.getIds() != null && !lbsTerminalStateDTOQuery.getIds().equals("")) {
            query.put("ids", lbsTerminalStateDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(lbsTerminalStateDTOQuery.getLikeName())) {
            query.put("likeName", lbsTerminalStateDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(lbsTerminalStateDTOQuery.getShowDate())) {
            query.put("showDate", lbsTerminalStateDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalStateDTOQuery.getStartDate())) {
            query.put("startDate", lbsTerminalStateDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalStateDTOQuery.getEndDate())) {
            query.put("endDate", lbsTerminalStateDTOQuery.getEndDate());
        }

        if (lbsTerminalStateDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", lbsTerminalStateDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(lbsTerminalStateDTOQuery.getOrderBy())) {
            query.put("orderBy", lbsTerminalStateDTOQuery.getOrderBy());
        }

        List<LbsTerminalStateDTO> list = convert(lbsTerminalStateService.selectByQuery(query));
        Page<LbsTerminalStateDTO> page = Page.from(lbsTerminalStateDTOQuery.getPn(), lbsTerminalStateDTOQuery.getPageSize(), list);
        page.setTotalItems(lbsTerminalStateService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return lbsTerminalStateService.deleteById(id);
        } catch (Exception e) {
            logger.error("LbsTerminalState delete failed.", e);
            throw new RuntimeException("LbsTerminalState delete failed, Please order your code.", e);
        }
    }
}